package com.news.web.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.Admin;
import com.news.service.AdminService;
import com.news.service.impl.AdminServiceImpl;

@WebServlet("/AdminAdminQueryServlet")
public class AdminAdminQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String adminName = request.getParameter("adminName");
		String adminPass = request.getParameter("adminPass");
		String adminAvatar = request.getParameter("adminAvatar");
		String adminIdStr = request.getParameter("adminId");
		
		int adminId = 0;
		if (adminIdStr != null && !adminIdStr.trim().isEmpty()) {
			try {
				adminId = Integer.parseInt(adminIdStr);
			} catch (NumberFormatException e) {
				// 可以处理参数不合法的情况，例如跳转到错误页面或者返回错误信息
				// 处理 adminIdStr 无法解析为整数的情况
				e.printStackTrace(); // 记录日志或者向用户显示错误信息
				request.setAttribute("adminQueryLikeList", null); // 或者设定为null
				request.getRequestDispatcher("/admin/adminAdmin.jsp").forward(request, response);
				return; // 结束方法，避免继续执行下面的逻辑
			}
		}
		
		Admin admin = new Admin(adminId, adminName, adminPass, adminAvatar);
		AdminService adminService = new AdminServiceImpl();
		List<Admin> adminQueryLikeList = adminService.likeSelectListAdmin(admin);

		request.setAttribute("adminQueryLikeList", adminQueryLikeList);
		request.getRequestDispatcher("/admin/adminAdmin.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
